 $(function(){
	 
 })
var iscreate=0;//检测是否生成图片完成
function getNowTime() {
	var d = new Date()
	var vYear = d.getFullYear()
	var vMon = d.getMonth() + 1
	var vDay = d.getDate()
	var h = d.getHours();
	var m = d.getMinutes();
	var se = d.getSeconds();
	s = vYear + (vMon < 10 ? "0" + vMon : vMon)+ (vDay < 10 ? "0" + vDay : vDay) + (h < 10 ? "0" + h : h)+ (m < 10 ? "0" + m : m) + (se < 10 ? "0" + se : se);
	return s
}
// 医院级别的选择
$('.form-level  label').on('click', function() {
	$('.form-level  label').siblings("label").removeClass('select');
	$(this).addClass('select');
	$('.form-level input').removeAttr('checked');
	var id = $(this).attr('for');
	$('#' + id).attr('checked', 'checked');
});
//医院类型的选择
$('.form-level-report-type label').on('click', function(){
    $('.form-level-report-type label').siblings("label").removeClass('select');
    $(this).addClass('select');
    $('.form-level-report-type input').removeAttr('checked');
    var id=$(this).attr('for');
    $("#"+ id).attr('checked','checked');
});

$("#"+tabId+" #generateAnalysisBtn").on("click",function() {
	loadTabParam(tabId);
	var param = jQuery.extend(true,{},paramTabPage[tabId]);
	var hospCode=param.hospCode; //医院院区编码
	var isBlank = false;
	$.each($('[required="required"]'), function(i, n) {
		if ($(n).val() == '') {
			setTimeout(function() {
				$(n).removeClass('ue-shake');
			}, 900);
			$(n).addClass('ue-shake');
			isBlank = true;
		}
	});
	if (isBlank)
		return;
	$("#"+tabId+" #generateAnalysisBtn").attr('disabled', 'disabled').find('i').addClass('fa-spin');
	$('#exportResult_analy .panel-footer-analy').html('<i class="fa fa-cog fa-spin fa-4x fa-fw margin-bottom text-success" style="margin-left:190px;" ></i>');
	var dateFrom = $('#'+tabId+' #sdate').val();
	var dateTo = $('#'+tabId+' #edate').val();
	var hospBuildYear = $('#'+tabId+' #hospBuildYear').val();
	var hospName=$('#'+tabId+' #hospName').val();
	var hospLevel= $('input[name="hospLevel"]:checked ').val();
	var hospType= $('input[name="hospType"]:checked ').val();
	var hospBedNum=$('#'+tabId+' #hospBedNum').val();
	var hospRealBedNum=$('#'+tabId+' #hospRealBedNum').val();
	var hospCaseNum=$('#'+tabId+' #hospCaseNum').val();
	var hospDocNum=$('#'+tabId+' #hospDocNum').val();
	var hospBedDaysNum=$('#'+tabId+' #hospBedDaysNum').val();
	var specCode=$('#'+tabId+' #specCodeID').val();
	var specDesc=$(".szySpecCode option:selected").text();
	var isInmedical= $("#"+tabId+" #isInmedical .radio_bg_check input").val();
	var isComplete= $("#"+tabId+" #isCompleteId .radio_bg_check input").val();
	var isRefine= $("#"+tabId+" #isRefineId .radio_bg_check input").val(); //是否精细化
	// 导出基础数据和表格信息
	console_wt('获取分析报告基础数据...');
		$.ajax({type : "post",
				url : contextPath+ "/analysisDRGsReport/findAnalysisReportInfo.dhc",
				data : {
					"dateFrom":dateFrom ,
					"dateTo": dateTo ,
					"hospName":hospName,
					"hospCode":hospCode,
					"hospBuildYear":hospBuildYear,
					"hospLevel":hospLevel,
					"hospType":hospType,
					"hospBedNum":hospBedNum,
					"hospRealBedNum":hospRealBedNum,
					"hospCaseNum":hospCaseNum,
					"hospDocNum":hospDocNum,
					"hospBedDaysNum":hospBedDaysNum,
					"specCode":specCode,
					"specDesc":specDesc,
					"isInmedical":isInmedical,
					"isComplete":isComplete,
					"isRefine":isRefine
				},
				success : function(result) {
					if (result.data.errorMessageData == undefined) {
						
						if (result.data.newdrgs == undefined|| result.data.newdrgs != 1) {
							var data = result.data;
							$("#t2").show();
							createPicture(data);
						
							var timer=setInterval(function(){
							    if (iscreate=='1') {
							     clearInterval(timer);
								$.ajax({   
									type : "post",
									url : contextPath+ "/analysisDRGsReport/createAnalysisReport.dhc",
									data : {"isInmedical":isInmedical },
									success : function(result) {
										if(result.success){
											var patch=result.data.patch;
											var hospname=result.data.hospname;
											var exportBtnHtml = '<div class="btn-group" role="group" style="margin-left: 35%;">';
											exportBtnHtml += '<a type="button" download="'+ hospname+ '住院病例DRG分析报告.docx" class="btn btn-default btn-lg" href="'+ patch+ '"><i class=" icon-save"></i>&nbsp;下载分析报告</a>';														
											exportBtnHtml += '</div>';
											$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled','disabled').find('i').removeClass('fa-spin');
											$('#exportResult_analy .panel-footer-analy').html(exportBtnHtml);
											drgUI.success('分析报告生成成功！');
										}else{
											var exportBtnHtml =result.errorMessage;
											$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled','disabled').find('i').removeClass('fa-spin');
											$('#exportResult_analy .panel-footer-analy').html(exportBtnHtml);	
											drgUI.error(result.errorMessage);
										}
									}
								});
							} 
							   else {
								$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
								$('#exportResult_analy .panel-footer-analy').html('<p class="text-danger">'+ data.errorMessage+ '</p>');
							}
							},1000)
						} else {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							   exportBtnHtml +='<table class="newGroup" style="line-height:26px">';
							   exportBtnHtml +='<caption style="color:red">新增病组：</caption>';
							for(var i=0;i<result.data.drgsList.length;i+=2){
								if(i == result.data.drgsList.length-1){
									exportBtnHtml += '<tr><td>'+ result.data.drgsList[i].PARDRG_CODE+ '</td><td>----</td><td>'+result.data.drgsList[i].PARDRG_CNT+'例</td></tr>';
								}else{
									exportBtnHtml += '<tr><td>'+ result.data.drgsList[i].PARDRG_CODE+ '</td><td>----</td><td>'+result.data.drgsList[i].PARDRG_CNT+'例</td><td>&nbsp;</td><td>'+ result.data.drgsList[i+1].PARDRG_CODE+ '</td><td>----</td><td>'+result.data.drgsList[i+1].PARDRG_CNT+'例</td></tr>';
								}
							}
							exportBtnHtml += '</table>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult_analy .panel-footer-analy').html(exportBtnHtml);
							drgUI.error('本院含有新增病组，请到标杆测算页面测算标杆并保存！');
						}
					} else {
						if (result.data.errorMessageData != undefined) {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							exportBtnHtml += '<span style="color:red;font-size:10px;">'+ result.data.errorMessageData+ '</span>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult_analy .panel-footer-analy').html(exportBtnHtml);
							drgUI.error(result.data.errorMessageData);
						} else {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							exportBtnHtml += '<span style="color:red;font-size:10px;">后台数据报错:'+result.data.errorMsg+'！</span>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateAnalysisBtn").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult_analy .panel-footer-analy').html(exportBtnHtml);
							drgUI.error(result.data.errorMsg);
						}
					}
				}
			});
});
// 生成分析报告所需图表
function createPicture(data) {
    iscreate=0;
	console_wt('开始生成图表...');
	chartData = [];
	 if(data.levOne!=0){
	    chartData.push({name : '一级',value : data.levOne})
	   }
	 if(data.levTwo!=0){
		    chartData.push({name : '二级',value : data.levTwo})
		   }
	 if(data.levThree!=0){
		    chartData.push({name : '三级',value : data.levThree})
		   }
	 if(data.levFour!=0){
		    chartData.push({name : '四级',value : data.levFour})
		   }
	 if(data.levOther!=0){
		    chartData.push({name : '其他',value : data.levOther})
		   }
	 analysisReportPieCharts(true,50,'picone',$('#imageOne'), chartData, "", "");
	 
	 console_wt('构建排名前二十名手术柱形图...');	
	 var xData = [];
	 var yData = [];
	 var topTwentyOper=data.topTwentyOperList
	 for (var i = 0; i < topTwentyOper.length; i++) {	
		 xData[i]=topTwentyOper[i].OPERNAME;
		 yData[i]=topTwentyOper[i].OPERCNT;	
	 }
	 analysisReportBarCharts('pictwo',$('#imageTwo'),xData,yData);
	 
	 console_wt('构建十八个重点手术柱形图...');	
	 var xData = [];
	 var yData = [];
	 var keyEighteenOper=data.keyEighteenOperList
	 for (var i = 0; i < keyEighteenOper.length; i++) {	
		 xData[i]=keyEighteenOper[i].OPERNAME;
		 yData[i]=keyEighteenOper[i].OPERCNT;
	 }
	 analysisReportBarCharts('picthree',$('#imageThree'),xData,yData);
	
	 console_wt('构建DRG病组类型分布图...');
	 chartData = [];
	 if(data.indrgc!=0){
		    chartData.push({name : '内科组',value : data.indrgc})
		   }
	 if(data.outdrgc!=0){
		    chartData.push({name : '外科组',value : data.outdrgc})
		   }
	 if(data.opdrgc!=0){
		    chartData.push({name : '操作组',value : data.opdrgc})
		   }
	analysisReportPieCharts(true,50,'picfour',$('#imageFour'), chartData, "", "");

	console_wt('构建DRG病例类型分布图...');
	chartData = [];
	if(data.incasec!=0){
		    chartData.push({name : '内科组',value : data.incasec})
		   }
	if(data.outcasec!=0){
		    chartData.push({name : '外科组',value : data.outcasec})
		   }
	if(data.opcasec!=0){
		    chartData.push({name : '操作组',value : data.opcasec})
		   }
	  analysisReportPieCharts(true,50,'picfive',$('#imageFive'), chartData, "", "");
	
	 console_wt('构建DRG病组复杂程度分布图...');
		chartData = [];
		 if(data.DRGsType1Num!=0){
			    chartData.push({name : '伴重要合并症和伴随病',value : data.DRGsType1Num})
			   }
		 if(data.DRGsType3Num!=0){
			    chartData.push({name : '伴一般合并症和伴随病',value : data.DRGsType3Num})
			   }
		 if(data.DRGsType5Num!=0){
			    chartData.push({name : '不伴合并症和伴随病',value : data.DRGsType5Num})
			   }
		 if(data.DRGsType9Num!=0){
			    chartData.push({name : '其他',value : data.DRGsType9Num})
			   }
		analysisReportPieCharts(true,31,'picsix',$('#imageSix'), chartData, "", "");

		console_wt('构建DRG病例复杂程度分布图...');
		chartData = [];
		 if(data.CaseType1Num!=0){
			    chartData.push({name : '伴重要合并症和伴随病',value : data.CaseType1Num})
			   }
		 if(data.CaseType3Num!=0){
			    chartData.push({name : '伴一般合并症和伴随病',value : data.CaseType3Num})
			   }
		 if(data.CaseType5Num!=0){
			    chartData.push({name : '不伴合并症和伴随病',value : data.CaseType5Num})
			   }
		 if(data.CaseType9Num!=0){
			    chartData.push({name : '其他',value : data.CaseType9Num})
			   }
	 analysisReportPieCharts(true,31,'picseven',$('#imageSeven'), chartData, "", "");
	 
	console_wt('构建RW分布面积图...');
	specCode="";
	var yData = [];
	var RWLevelCnt=data.RWLevelCntList
	for (var i = 0; i < RWLevelCnt.length; i++) {	
		yData[i]=RWLevelCnt[i].RWcnt;
	}
	analysisReportAcreageCharts('piceight',$('#imageEight'),yData);
	
	console_wt('构建总权重排名前十的临床专业CMI和DRG组数分布图...');
	var bubbleData = [];
	var maxTotalWeight = 1;
	var specDRGsAndCMI=data.specDRGsAndCMIList
	for (var i = 0; i < specDRGsAndCMI.length; i++) {	
		bubbleData[i]=[specDRGsAndCMI[i].CMI,specDRGsAndCMI[i].DRGSCNT,specDRGsAndCMI[i].TOTALWEIGHT,specDRGsAndCMI[i].SPEC_DESC];
		if(specDRGsAndCMI[i].TOTALWEIGHT != null && specDRGsAndCMI[i].TOTALWEIGHT>maxTotalWeight){	
			maxTotalWeight = specDRGsAndCMI[i].TOTALWEIGHT;
		}
	}
	analysisReportBubbleCharts('picnine',$('#imageNine'),bubbleData,maxTotalWeight);
	
	console_wt('构建总权重排名前十的临床专业DRG病组复杂程度分布图...');
	var DRGs9Data = [];
	var DRGs7Data = [];
	var DRGs5Data = [];
	var DRGs3Data = [];
	var DRGs1Data = [];
	var cmiData = [];
	var specNameData = [];
	var specDRGsFZCD=data.specDRGsAndCMIList
	for (var i = 0; i < specDRGsFZCD.length; i++) {	
		specNameData[i] = specDRGsFZCD[i].SPEC_DESC;
		cmiData[i] = specDRGsFZCD[i].CMI;
		DRGs1Data[i] = specDRGsFZCD[i].DRGS1CNT;
		DRGs3Data[i] = specDRGsFZCD[i].DRGS3CNT;
		DRGs5Data[i] = specDRGsFZCD[i].DRGS5CNT;
		DRGs7Data[i] = specDRGsFZCD[i].DRGS7CNT;
		DRGs9Data[i] = specDRGsFZCD[i].DRGS9CNT;
	}
	analysisReportStactCharts('picten',$('#imageTen'),specNameData,DRGs1Data,DRGs3Data,DRGs5Data,DRGs7Data,DRGs9Data,cmiData);
	 
	console_wt('构建各临床专业费用和时间消耗指数象限分布图...');
	chartData = [];
		var xyAxis = {"xAxis": {"code": "timeEff", "desc": "时间消耗指数"}, "yAxis": {"code": "feeEff", "desc": "费用消耗指数"}}
		if(data.depFeeAndTimeEffMapList.length==0){
			chartData.push({
				name:"",
				timeEff:"0",
				feeEff:"0"
			});
		}else{
			for (var i = 0; i < data.depFeeAndTimeEffMapList.length; i++) {
				chartData.push({
					name:"",
					timeEff:data.depFeeAndTimeEffMapList[i].timeEff,
					feeEff:data.depFeeAndTimeEffMapList[i].feeEff
				});
			}
		}
	analysisReportScatterChart(true,'piceleven', $('#imageEleven'), xyAxis, chartData, "","",1,1); 
	
	console_wt('构建某个重点临床专业RW分布面积图...');
	var yData = [];
	var specRWLevelCnt=data.specRWLevelCntList
	for (var i = 0; i < specRWLevelCnt.length; i++) {	
		yData[i]=specRWLevelCnt[i].RWcnt;
	}
	analysisReportAcreageCharts('pictwelve',$('#imageTwelve'),yData);
	
	console_wt('构建某个重点临床专业各手术级别病例分布图...');
	chartData = [];
	 if(data.specCodeLevelOne!=0){
		    chartData.push({name : '一级',value : data.specCodeLevelOne})
		   }
	 if(data.specCodeLevelTwo!=0){
		    chartData.push({name : '二级',value : data.specCodeLevelTwo})
		   }
	 if(data.specCodeLevelThr!=0){
		    chartData.push({name : '三级',value : data.specCodeLevelThr})
		   }
	 if(data.specCodeLevelFour!=0){
		    chartData.push({name : '四级',value : data.specCodeLevelFour})
		   }
	 if(data.specCodeLevelOth!=0){
		    chartData.push({name : '其他',value : data.specCodeLevelOth})
		   }
	 analysisReportPieCharts(true,31,'picthirteen',$('#imageThirteen'), chartData, "", "");
	
	console_wt('构建某个重点临床专业例数最多的十种手术...');
	var xData = [];
	var yData = [];
	var specTopTenOper=data.specTopTenOperList
	for (var i = 0; i < specTopTenOper.length; i++) {	
		xData[i]=specTopTenOper[i].OPERNAME;
		yData[i]=specTopTenOper[i].OPERCNT;
	}
	analysisReportTranBarCharts('picfourteen',$('#imageFourteen'),xData,yData);
	 
	console_wt('构建DRG病例未入组原因分布图...');
	chartData = [];
	for (var i = 0; i < data.notInGroupReasonList.length; i++) {
		chartData.push({
			name: data.notInGroupReasonList[i].DECC,
			value: data.notInGroupReasonList[i].CNT
		});
	}
    analysisReportPieCharts(true,50,'picfifteen',$('#imageFifteen'), chartData, "", "");
	
	iscreate=1;
	console_wt('图表构建完毕！');
	return true;
}
function console_wt(s) {
	$('#exportResult_analy .panel-footer-analy').append('<p style="text-align:center;">' + s + '</p>');
}